﻿namespace EFCoreApp;

public class TaskClass
{
    static int count = 1;
    public static void MainTest()
    {
        Task.Run(PrintOddNumbers);
        // 这里改成这个也可以
        // var thread1 = new Thread(PrintOddNumbers);
        Task.Run(PrintEvenNumbers);
        Console.ReadLine();
    }
    //如果用Thread改成同步方法
    static async Task PrintOddNumbers()
    {
        while (count <= 100)
        {
            if (count % 2 == 1)
            {
                Console.WriteLine("Thread 1: " + count);
                count++;
                //如果用Thread这里改成 Thread.Sleep(1);
                await Task.Delay(1);
            }
        }
    }
    static async Task PrintEvenNumbers()
    {
        while (count <= 100)
        {
            if (count % 2 == 0)
            {
                Console.WriteLine("Thread 2: " + count);
                count++;
                await Task.Delay(1);
            }
        }
    }
}